var vue = new Vue({
    el:"#app",
    data:{
        title:"MoMo登录",
        errmsg:"",
        submitFlag:true,
        user:{
            account:"",
            password:"",
            code:""
        }
    },
    mounted(){
        document.onkeydown = (e)=>{
            if(e.keyCode == 13){
                this.toLogin();
            }
        }
    },
    computed:{ //1 <- N

    },
    watch:{ //1 --> N
        errmsg:{
            handler(newval,oldval){
                if(this.stimer)clearTimeout(this.stimer);
                this.stimer = setTimeout(() => {
                    this.errmsg = "";
                },3000)
            }
        }
    },
    methods:{
        toLogin:throttle(function(){
            console.log("okokokokokokokok")
            var that = this;
            var user = that.user;
            if(!user.account){
                that.errmsg = "请输入账号"
                document.getElementById("account").focus();
                return;
            }

            if(!user.password){
                that.errmsg = "请输入密码"
                document.getElementById("pwd").focus();
                return;
            }

            if(!user.code){
                that.errmsg ="请输入验证码"
                document.getElementById("code").focus();
                return;
            }


            axios.post("/logined",user).then(res => {
                if(res.data == "success"){
                    window.location.href = "/";
                }else if(res.data=="failcode"){
                    that.errmsg ="你的验证码输入有误"
                    that.user.code = "";
                    document.getElementById("code").value = "";
                    document.getElementById("code").focus();
                    document.getElementById("ksdimgcode").src = "/kaptcha?d="+new Date().getTime();
                }else{
                    that.errmsg ="输入账号密码有误"
                    that.user.password = "";
                    document.getElementById("pwd").value = "";
                    document.getElementById("pwd").focus();
                }
            })
        },5000)
    }
});


